home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / pippki.jar / content / pippki / pippki.js < prev    next >
Encoding:
JavaScript  |  2007-10-01  |  6.8 KB  |  195 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is mozilla.org code.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Javier Delgadillo <javi@netscape.com>
  25.  *   Kaspar Brand <mozcontrib@velox.ch>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. /*
  42.  * These are helper functions to be included
  43.  * pippki UI js files.
  44.  */
  45.  
  46. function setText(id, value) {
  47.   var element = document.getElementById(id);
  48.   if (!element) return;
  49.      if (element.hasChildNodes())
  50.        element.removeChild(element.firstChild);
  51.   var textNode = document.createTextNode(value);
  52.   element.appendChild(textNode);
  53. }
  54.  
  55. const nsICertificateDialogs = Components.interfaces.nsICertificateDialogs;
  56. const nsCertificateDialogs = "@mozilla.org/nsCertificateDialogs;1"
  57.  
  58. function viewCertHelper(parent, cert) {
  59.   if (!cert)
  60.     return;
  61.  
  62.   var cd = Components.classes[nsCertificateDialogs].getService(nsICertificateDialogs);
  63.   cd.viewCert(parent, cert);
  64. }
  65.  
  66. function getDERString(cert)
  67. {
  68.   var length = {};
  69.   var derArray = cert.getRawDER(length);
  70.   var derString = '';
  71.   for (var i = 0; i < derArray.length; i++) {
  72.     derString += String.fromCharCode(derArray[i]);
  73.   }
  74.   return derString;
  75. }
  76.  
  77. function getPKCS7String(cert, chainMode)
  78. {
  79.   var length = {};
  80.   cert.QueryInterface(Components.interfaces.nsIX509Cert3);
  81.   var pkcs7Array = cert.exportAsCMS(chainMode, length);
  82.   var pkcs7String = '';
  83.   for (var i = 0; i < pkcs7Array.length; i++) {
  84.     pkcs7String += String.fromCharCode(pkcs7Array[i]);
  85.   }
  86.   return pkcs7String;
  87. }
  88.  
  89. function getPEMString(cert)
  90. {
  91.   var derb64 = btoa(getDERString(cert));
  92.   // Wrap the Base64 string into lines of 64 characters, 
  93.   // with CRLF line breaks (as specified in RFC 1421).
  94.   var wrapped = derb64.replace(/(\S{64}(?!$))/g, "$1\r\n");
  95.   return "-----BEGIN CERTIFICATE-----\r\n"
  96.          + wrapped
  97.          + "\r\n-----END CERTIFICATE-----\r\n";
  98. }
  99.  
  100. function alertPromptService(title, message)
  101. {
  102.   var ps = null;
  103.   var ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
  104.            getService(Components.interfaces.nsIPromptService);
  105.   ps.alert(window, title, message);
  106. }
  107.  
  108. function exportToFile(parent, cert)
  109. {
  110.   var bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  111.   if (!cert)
  112.     return;
  113.  
  114.   var nsIFilePicker = Components.interfaces.nsIFilePicker;
  115.   var fp = Components.classes["@mozilla.org/filepicker;1"].
  116.            createInstance(nsIFilePicker);
  117.   fp.init(parent, bundle.GetStringFromName("SaveCertAs"),
  118.           nsIFilePicker.modeSave);
  119.   var filename = cert.commonName;
  120.   if (!filename.length)
  121.     filename = cert.windowTitle;
  122.   // remove all whitespace from the default filename
  123.   fp.defaultString = filename.replace(/\s*/g,'');
  124.   fp.defaultExtension = "crt";
  125.   fp.appendFilter(bundle.GetStringFromName("CertFormatBase64"), "*.crt; *.pem");
  126.   fp.appendFilter(bundle.GetStringFromName("CertFormatBase64Chain"), "*.crt; *.pem");
  127.   fp.appendFilter(bundle.GetStringFromName("CertFormatDER"), "*.der");
  128.   fp.appendFilter(bundle.GetStringFromName("CertFormatPKCS7"), "*.p7c");
  129.   fp.appendFilter(bundle.GetStringFromName("CertFormatPKCS7Chain"), "*.p7c");
  130.   fp.appendFilters(nsIFilePicker.filterAll);
  131.   var res = fp.show();
  132.   if (res != nsIFilePicker.returnOK && res != nsIFilePicker.returnReplace)
  133.     return;
  134.  
  135.   var content = '';
  136.   switch (fp.filterIndex) {
  137.     case 1:
  138.       content = getPEMString(cert);
  139.       var chain = cert.getChain();
  140.       for (var i = 1; i < chain.length; i++)
  141.         content += getPEMString(chain.queryElementAt(i, Components.interfaces.nsIX509Cert));
  142.       break;
  143.     case 2:
  144.       content = getDERString(cert);
  145.       break;
  146.     case 3:
  147.       content = getPKCS7String(cert, Components.interfaces.nsIX509Cert3.CMS_CHAIN_MODE_CertOnly);
  148.       break;
  149.     case 4:
  150.       content = getPKCS7String(cert, Components.interfaces.nsIX509Cert3.CMS_CHAIN_MODE_CertChainWithRoot);
  151.       break;
  152.     case 0:
  153.     default:
  154.       content = getPEMString(cert);
  155.       break;
  156.   }
  157.   var msg;
  158.   var written = 0;
  159.   try {
  160.     var file = Components.classes["@mozilla.org/file/local;1"].
  161.                createInstance(Components.interfaces.nsILocalFile);
  162.     file.initWithPath(fp.file.path);
  163.     var fos = Components.classes["@mozilla.org/network/file-output-stream;1"].
  164.               createInstance(Components.interfaces.nsIFileOutputStream);
  165.     // flags: PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE
  166.     fos.init(file, 0x02 | 0x08 | 0x20, 00644, 0);
  167.     written = fos.write(content, content.length);
  168.     fos.close();
  169.   }
  170.   catch(e) {
  171.     switch (e.result) {
  172.       case Components.results.NS_ERROR_FILE_ACCESS_DENIED:
  173.         msg = bundle.GetStringFromName("writeFileAccessDenied");
  174.         break;
  175.       case Components.results.NS_ERROR_FILE_IS_LOCKED:
  176.         msg = bundle.GetStringFromName("writeFileIsLocked");
  177.         break;
  178.       case Components.results.NS_ERROR_FILE_NO_DEVICE_SPACE:
  179.       case Components.results.NS_ERROR_FILE_DISK_FULL:
  180.         msg = bundle.GetStringFromName("writeFileNoDeviceSpace");
  181.         break;
  182.       default:
  183.         msg = e.message;
  184.         break;
  185.     }
  186.   }
  187.   if (written != content.length) {
  188.     if (!msg.length)
  189.       msg = bundle.GetStringFromName("writeFileUnknownError");
  190.     alertPromptService(bundle.GetStringFromName("writeFileFailure"),
  191.                        bundle.formatStringFromName("writeFileFailed",
  192.                          [ fp.file.path, msg ], 2));
  193.   }
  194. }
  195.